/**
 * @ngdoc function
 * @name app.config:uiRouter
 * @description
 * # Config
 * Config for the router
 */
(function() {
    'use strict';
    angular
      .module('app')
      .run(runBlock)
      .config(config);

      runBlock.$inject = ['$rootScope', '$state', '$stateParams'];
      function runBlock($rootScope,   $state,   $stateParams) {
          $rootScope.$state = $state;
          $rootScope.$stateParams = $stateParams;
      }

      config.$inject =  ['$stateProvider', '$urlRouterProvider','$httpProvider', 'MODULE_CONFIG', '$locationProvider','ngClipProvider'];
      function config( $stateProvider,   $urlRouterProvider,$httpProvider,MODULE_CONFIG, $locationProvider ,ngClipProvider) {

        var p = getParams('layout'),
            l = p ? p + '.' : '',
            layout = '../views/layout/layout.html',
            index = '../views/mxu/home/index.html';

        $urlRouterProvider
  	      .when('/app/home','/app/home/index')

          .otherwise('/app/home/index');
        $stateProvider
          .state('app', {
            abstract: true,
            url: '/app',
            views: {
              '': {
                templateUrl: layout,
                resolve: load([ 'ui.bootstrap','angularFileUpload'])
              }
            }
          })
			/****首页start****/
          	.state('app.home', {
              url: '/home',
              template: '<div ui-view></div>',
            })
            .state('app.home.index', {
              url: '/index?remember',
              templateUrl: index,
              data : { title: '首页',mark:'index'},
              controller: "homeCtrl",
              cache:'false',
              resolve: load(['scripts/mxu/home/script.js','../assets/styles/mxu/home/style.css'])
            })
			.state('app.home.log', {
              url: '/log',
              templateUrl: "../views/mxu/home/logs.html",
              data : { title: '首页/动态',mark:'index' },
              controller: "logCtrl",
              resolve: load([
              	'../assets/styles/mxu/home/style.css',
              	'scripts/mxu/home/log.js'
              ])
            })

			/****首页end****/

            /**error**/
            .state('404', {
                url: '/404?status',
                controller: "errorCtrl",
                templateUrl: '../views/mxu/common/error.html',
                resolve: load(['scripts/mxu/common/tpls/error.js'])
            })
            /**error end**/
            .state('member', {
                url: '/member',
                template: '<div class="dark bg-auto w-full"><div ui-view class="fade-in-right-big smooth pos-rlt"></div></div>'
            })

            .state('app.demo', {
              url: '/demo',
              templateUrl: '../views/mxu/demo/demo.html',
              data : { title: 'demo' },
              controller: "demoCtrl",
              resolve: load(['scripts/mxu/demo/script.js','../assets/styles/mxu/demo/style.css'])
            });


        function load(srcs, callback) {
          return {
              deps: ['$ocLazyLoad', '$q',
                function( $ocLazyLoad, $q ){
                  var deferred = $q.defer();
                  var promise  = false;
                  srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                  if(!promise){
                    promise = deferred.promise;
                  }
                  angular.forEach(srcs, function(src) {
                    promise = promise.then( function(){
                      angular.forEach(MODULE_CONFIG, function(module) {
                        if( module.name == src){
                          src = module.module ? module.name : module.files;
                        }
                      });
                      return $ocLazyLoad.load(src);
                    } );
                  });
                  deferred.resolve();
                  return callback ? promise.then(function(){ return callback(); }) : promise;
              }]
          }
        }

        function getParams(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }

        $httpProvider.defaults.withCredentials = true;
        ngClipProvider.setPath("../libs/js/zeroClipboard/ZeroClipboard.swf");
        $locationProvider.html5Mode(true);
      }

})();
